package com.example.controller;

import com.example.common.Result;
import com.example.pojo.Collection;
import com.example.service.CollectionService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    private CollectionService collectionService;


    /**
     * 添加收藏
     * @param collection
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Collection collection) {
        collectionService.add(collection);
            return Result.success();
    }


    @GetMapping("/isCollection")
    public Result isCollection(@RequestParam Integer userId,@RequestParam Integer roomId) {
       Boolean result = collectionService.isCollection(userId,roomId);
        return Result.success(result);
    }


    /**
     * 删除收藏
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer userId,@RequestParam Integer roomId) {
        collectionService.deleteById(userId,roomId);
        return Result.success();
    }


    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询收藏
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Collection collection = collectionService.selectById(id);
        return Result.success(collection);
    }

    /**
     * 查询所有收藏
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Collection collection) {
        List<Collection> list = collectionService.selectAll(collection);
        return Result.success(list);
    }



    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(Collection collection,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Collection> pageInfo= collectionService.selectPage(collection,pageNum,pageSize);
        return Result.success(pageInfo);
    }

}
